package io.renren.modules.avanotes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.config.BizConfig;
import io.renren.config.TXYCosConfig;
import io.renren.modules.avanotes.dao.SubtitleDetailsDao;
import io.renren.modules.avanotes.entity.SubtitleDetailsEntity;
import io.renren.modules.avanotes.service.SubtitleDetailsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

//@Service("subtitleDetailsService")//默认只有一个实现，那就不写名称了直接使用默认的名称！
@Service
public class SubtitleDetailsServiceImpl extends ServiceImpl<SubtitleDetailsDao, SubtitleDetailsEntity> implements SubtitleDetailsService {

    @Value("${resource.pre.path}")
    private String resourcePathPre;

    @Autowired
    private TXYCosConfig config;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SubtitleDetailsEntity> page = this.page(
                new Query<SubtitleDetailsEntity>().getPage(params),
                new QueryWrapper<SubtitleDetailsEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 首字母大写的单词效果更好！
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils searchPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<SubtitleDetailsEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(key)) {
            queryWrapper.like(SubtitleDetailsEntity.CN_ENGLISH, key).or().like(SubtitleDetailsEntity.CN_ENGLISH, key.trim());
        }
        IPage<SubtitleDetailsEntity> page = this.page(
                new Query<SubtitleDetailsEntity>().getPage(params),
                queryWrapper
        );

        //替换路径
        List<SubtitleDetailsEntity> records = page.getRecords();
        for (SubtitleDetailsEntity record : records) {
            //浏览器无法支持本地路径
            //record.setPath("file://" + record.getPath().replace("\\", "/"));
            //本地路径
            //String newPath = record.getPath().replace("\\", "/");
            //E:/File
            //newPath = newPath.replace("E:/File", resourcePathPre + BizConfig.PATH_RESOURCE_RELATIVE_PRE);
            if (StringUtils.isNotBlank(record.getCoskey())) {
                String newPath = config.getCConfig().getQcloudDomain() + record.getCoskey();
                record.setPath(newPath);
            }
        }
        return new PageUtils(page);
    }


}